package top.lilele.adminSystem.utils;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import cn.hutool.extra.template.Template;
import cn.hutool.extra.template.TemplateConfig;
import cn.hutool.extra.template.TemplateEngine;
import cn.hutool.extra.template.TemplateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Maps;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import top.lilele.adminSystem.config.MailConfig;
import top.lilele.adminSystem.dto.AddUserDto;
import top.lilele.adminSystem.entity.AdminNoticeEntity;
import top.lilele.adminSystem.handler.WebSocketHandler;
import top.lilele.adminSystem.mapper.AdminUserMapper;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * 异步操作
 *
 * @author lilele
 */
@Service
public class AsyncOperate {
    @Resource
    AdminUserMapper adminUserMapper;

    @Resource
    MailConfig mailConfig;

    /**
     * websocket发送公告
     *
     * @param adminNoticeEntity
     */
    @Async("asyncTaskExecutor")
    public void sendWebsocketMessageAsync(AdminNoticeEntity adminNoticeEntity) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", adminNoticeEntity.getCreatedId());
        String userName = adminUserMapper.selectOne(queryWrapper).getTrueName();
        String message = "【" + userName + "】发送了一条公告<br/>" + "【公告标题】" + adminNoticeEntity.getTitle();
        BaseResponse baseResponse = BaseResponse.info(ResponseCodeEnum.Success, message);
        WebSocketHandler.sendEveryUserMessage(baseResponse, StpUtil.getTokenValueByLoginId(adminNoticeEntity.getCreatedId()));
    }

    /**
     * 发送添加用户邮箱
     */
    @Async("asyncTaskExecutor")
    public void sendAddUserMail(AddUserDto addUserDto) {
        MailAccount mailAccount = new MailAccount();
        mailAccount.setHost(mailConfig.getHost());
        mailAccount.setFrom(mailConfig.getFrom());
        mailAccount.setPort(mailConfig.getPort());
        mailAccount.setPass(mailConfig.getPass());
        TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig("templates", TemplateConfig.ResourceMode.CLASSPATH));
        Template template = engine.getTemplate("addUserEmail.ftl");
        HashMap<Object, Object> maps = Maps.newHashMap();
        maps.put(Utils.getName(AddUserDto::getUsername), addUserDto.getUsername());
        maps.put(Utils.getName(AddUserDto::getPassword), addUserDto.getPassword());
        String result = template.render(maps);
        MailUtil.send(mailAccount, addUserDto.getEmail(), "OA系统发送邀请内容", result, true, null);
    }

    @Async("asyncTaskExecutor")
    public void sendResourceChangeWebsocketAsync(AdminNoticeEntity adminNoticeEntity) {
        String message = "权限有变更";
        BaseResponse baseResponse = BaseResponse.info(ResponseCodeEnum.Success, message);
        WebSocketHandler.sendEveryUserMessage(baseResponse);
    }
}
